<?php

use Biz\model\brand\Brand;
use Biz\model\brandDecoration\BrandCard;
use Biz\model\brandDecoration\BrandCardListing;
use Biz\pockets\constInterface\SourceRightsInterface;
use Biz\service\BrandRights\BrandEquityVerificationService;
use Biz\service\wiki\WikiContent;
use Biz\service\wikiRanking\Ranking;
use Doraemon\cache\ShortLink\ShortLinkCounter;
use Doraemon\service\statSign\Basic as statSign;
use Doraemon\tools\BlockType;
use Doraemon\tools\Hhzurl;
use Doraemon\tools\ImgUrl;
use Doraemon\tools\HhzNiceLivingUrl;

class DecorationInfoAction extends ActionPlugin
{

    const USER_LEVEL = self::LEVEL_NOTHING;
    const SHORT_PREFIX = 'http://l.hhz1.cn/';

    public function main()
    {
        $uid = $this->getRequest()->getQuery('uid', 0);
        $brandInfo = Brand::getBrandInfoByUid($uid);
        $brand_id = $brandInfo ? $brandInfo['id'] : 0;
        $brandType = $brandInfo ? $brandInfo['brand_type'] : 0;
        $cardList = BrandCard::getBrandCards($brand_id);
        $cardInfos = BrandCardListing::getCardInfosForApp($brand_id);
        $infos = [];
        if ($cardInfos && is_array($cardInfos)) {
            foreach ($cardInfos as $cardInfo) {
                $cardInfo['wiki_img_list'] = [];
                $cardInfo['pic_url'] = ImgUrl::getUrl(ImgUrl::APPTYPE_BIZ, $cardInfo['pic_id'],
                    ImgUrl::IMAGE_SIZE_ORI_NPHONE);
                $getUrl = $this->getLink($cardInfo);
                $cardInfo['link_h5'] = (string)$getUrl['link_h5'];
                $cardInfo['link'] = (string)$getUrl['link'];
                if ($cardInfo['link_params']) {
                    $cardInfo['wiki_img_list'] = WikiContent::getWikiList($cardInfo['link_params'], $brand_id);
                }
                $cardInfo['statSign'] = statSign::genStatSign(statSign::MODULE_USER_HOMEPAGE_BRAND_DECORATION_BANNER, [
                    statSign::EXTEND_KEY_CONFIG_ID => $cardInfo['id'],
                    statSign::EXTEND_KEY_URL => $cardInfo['link']
                ], ['brand_id' => $cardInfo['brand_id'], 'card_id' => $cardInfo['card_id']]);
                $infos[$cardInfo['card_id']][] = $cardInfo;
            }
        }

        $cards = [];
        if ($cardList && is_array($cardList)) {
            foreach ($cardList as $k => $card) {
                $card_type = $card['card_type'];


                //校验 卡片 a-c 视频卡片
                if (array_key_exists($card_type, SourceRightsInterface::DECORATION)) {
                    $res = BrandEquityVerificationService::checkOneSourceStatus($brand_id, SourceRightsInterface::DECORATION[$card_type], true);
                    if (!$res) {
                        continue;
                    }
                }

                if ($infos[$card['id']]) {
                    $cards[$k]['type'] = (int)$card['card_type'];
                    if ($card['card_type'] != 1) {
                        $cards[$k]['title'] = $card['card_title'];
                    }
                    $cards[$k]['card_id'] = $card['id'];
                    if (is_array($infos[$card['id']])) {
                        if ($card['card_type'] != BrandCard::CARD_TYPE_C) {
                            foreach ($infos[$card['id']] as &$info) {
                                unset($info['wiki_img_list']);
                            }
                        }
                    }

                    $cards[$k]['banner_list'] = \Doraemon\tools\StatSign::versionCompatible($infos[$card['id']],\Doraemon\tools\StatSign::BRAND_UP_GRADE_VERSION_521, BlockType::TYPE_CAROUSEL_BANNER, 'brand_banner');
                }
            }
        }
        $data['rows'] = array_values($cards);

        //品牌荣耀榜
        $ranking_info = Ranking::getBrandGlory($brand_id);
        if ($ranking_info && is_array($ranking_info)) {
            $data['ranking_info'] = $ranking_info;
        }
        $brandActivityInfo = \Biz\service\brand\BrandActivity::getBrandActivityInfo($brand_id);
        if ($brandActivityInfo) {
            $data['brand_activity'] = $brandActivityInfo;
        }
        $this->setSuccess($data);
    }

    public function getLink($cardInfo = [])
    {

        if ($cardInfo && is_array($cardInfo) && isset($cardInfo['link_type']) && isset($cardInfo['link_params'])) {
            switch ($cardInfo['link_type']) {
                case 1:
//                    $link = 'hhz://note:{"note_id":"' . $cardInfo['link_params'] . '"}';
                    $link =   HhzNiceLivingUrl::getNoteDetailLink($cardInfo['link_params']);
                    $linkH5 = Hhzurl::h5PhotoShow($cardInfo['link_params']);
                    break;
                case 2:
                    //文章
                //    $link = 'hhz://article:' . $cardInfo['link_params'];
                    $link =   HhzNiceLivingUrl::getArticleDetailLink($cardInfo['link_params']);
                    $linkH5 = Hhzurl::h5BangyangShow($cardInfo['link_params']);
                    break;
                case 3:
                    //毛坯
                  //  $link = 'hhz://blank:' . $cardInfo['link_params'];
                    $link=HhzNiceLivingUrl::getBlankDetailLink($cardInfo['link_params']);
                    $linkH5 = Hhzurl::h5BlankShow($cardInfo['link_params']);
                    break;
                case 4:
                    //商品详情
                 //   $link = 'hhz://wikiDetail:' . $cardInfo['link_params'];
                    $link =   HhzNiceLivingUrl::getWikiDetailLink($cardInfo['link_params']);
                    $linkH5 = Hhzurl::H5WikiDetails($cardInfo['link_params']);
                    break;
                case 5:
                case 6:
                    //跳转连接
                    $url = $cardInfo['link_params'];
                    if ($cardInfo['link_module'] != 0) {
                        $url_short = crc32($url);
                        $result = sprintf("%u", $url_short);
                        $result = $this->code62($result);
                        $link = [
                            'h5' => $url,
                            'app' => json_encode([
                                "code" => 1,
                                "data" => [
                                    "type" => "link",
                                    "link" => ''
                                ]
                            ]),
                        ];
                        $link_json = json_encode($link);
                        $has_link = ShortLinkCounter::getlink($result);
                        if (!$has_link) {
                            ShortLinkCounter::set($link_json, $result);
                            ShortLinkCounter::getlink($result);
                        }
                        $url = self::SHORT_PREFIX . $result . '?hhzjump=1';

                    }

                    $parseUrl = parse_url($url);
                    $urlQuery = $parseUrl['query'];
                    parse_str($urlQuery, $urlParams);
                    $urlParams['holdup'] = 1;
                    $urlQuery = http_build_query($urlParams);
                    $url = ($parseUrl['scheme'] ? $parseUrl['scheme'] : 'https') . '://' . $parseUrl['host'] . $parseUrl['path'] . '?' . $urlQuery;

//                    $url = base64_encode($url);
//                    $link = 'hhz://urlb:' . $url;
                      $link = \Biz\tools\helper\Common::formatLinkNormalLink($url);
                    $linkH5 = '';
                    break;
                default:
                    $link = '';
                    $linkH5 = '';
            }
        }

        $linkInfo = ['link' => $link, 'link_h5' => $linkH5];
        return $linkInfo;
    }

    //生成唯一的短连接
    public function code62($x)
    {
        $show = '';
        while ($x > 0) {
            $s = $x % 62;
            if ($s > 35) {
                $s = chr($s + 61);
            } elseif ($s > 9 && $s <= 35) {
                $s = chr($s + 55);
            }
            $show .= $s;
            $x = floor($x / 62);
        }
        return $show;
    }

}
